package com.example.a28796.beautiful.zhangyuqiadapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.a28796.beautiful.R;
import com.example.a28796.beautiful.zhangyuqibean.BeautyLocalBean;
import com.example.a28796.beautiful.zhangyuqibean.BeautyYouLikeBean;

import org.xutils.x;

import java.util.List;

/**
 * Created by admin on 2016/8/30.
 */
public class BeautyListAdapter extends BaseAdapter {
    private Context mContext;
    private List<BeautyYouLikeBean.ObjBean.ListDataBean> youLike;
    private List<BeautyLocalBean.ObjBean.ListDataBean> local;

    public BeautyListAdapter(Context mContext, List<BeautyYouLikeBean.ObjBean.ListDataBean> youLike, List<BeautyLocalBean.ObjBean.ListDataBean> local) {
        this.mContext = mContext;
        this.youLike = youLike;
        this.local = local;
    }

    @Override
    public int getCount() {
        return youLike.size() + local.size();
    }

    @Override
    public Object getItem(int position) {
        if (position >= 0 && position < youLike.size()) {
            return youLike.get(position);
        } else {
            return local.get(position - youLike.size());
        }
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position >= 0 && position < youLike.size()) {
            ViewholderOne holderOne = null;
            convertView = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_beautybuy_one, null);
                holderOne = new ViewholderOne(convertView);
                convertView.setTag(holderOne);
            } else {
                holderOne = (ViewholderOne) convertView.getTag();
            }
            BeautyYouLikeBean.ObjBean.ListDataBean listDataBean = youLike.get(position);
            if (position == 0) {
                holderOne.mlayoutOne.setVisibility(View.VISIBLE);
            } else {
                holderOne.mlayoutOne.setVisibility(View.GONE);
            }
            if (position == youLike.size() - 1) {
                holderOne.buttonOne.setVisibility(View.VISIBLE);
            } else {
                holderOne.buttonOne.setVisibility(View.GONE);
            }
            holderOne.title1.setText(listDataBean.getTitle());
            holderOne.title2.setText(listDataBean.getHospital().getHospitalName());
            holderOne.title3.setText("已售" + listDataBean.getStockNum().getSaleNum() + "件");
            holderOne.title4.setText("¥" + listDataBean.getSalePrice() + "元");
            holderOne.title5.setText(listDataBean.getOrigPrice() + "");
            x.image().bind(holderOne.imageViewOne, listDataBean.getCover().getUrl());

        } else {
            int newposition = position - youLike.size();
            ViewholderTwo holderTwo = null;
            convertView = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_beautybuy_two, null);
                holderTwo = new ViewholderTwo(convertView);
                convertView.setTag(holderTwo);
            } else {
                holderTwo = (ViewholderTwo) convertView.getTag();
            }
            BeautyLocalBean.ObjBean.ListDataBean listDataBean = local.get(newposition);
            if (newposition == 0) {
                holderTwo.mlayoutTwo.setVisibility(View.VISIBLE);
            } else {
                holderTwo.mlayoutTwo.setVisibility(View.GONE);
            }
            if (newposition == local.size() - 1) {
                holderTwo.buttonTwo.setVisibility(View.VISIBLE);
            } else {
                holderTwo.buttonTwo.setVisibility(View.GONE);
            }
            holderTwo.title3.setText(listDataBean.getHospital().getHospitalName());
            holderTwo.title4.setText(listDataBean.getHospital().getType());
            holderTwo.title5.setText(listDataBean.getStock().get(0).getTitle());
            holderTwo.title6.setText(listDataBean.getStock().get(1).getTitle());
            x.image().bind(holderTwo.imageViewTwo, listDataBean.getHospital().getHeadUrl());
        }

        return convertView;
    }

    public static class ViewholderOne {
        private TextView title1, title2, title3, title4, title5;
        private ImageView imageViewOne;
        private LinearLayout mlayoutOne;
        private Button buttonOne;

        public ViewholderOne(View view) {
            title1 = (TextView) view.findViewById(R.id.tv_itemone_title1);
            title2 = (TextView) view.findViewById(R.id.tv_itemone_title2);
            title3 = (TextView) view.findViewById(R.id.tv_itemone_title3);
            title4 = (TextView) view.findViewById(R.id.tv_itemone_title4);
            title5 = (TextView) view.findViewById(R.id.tv_itemone_title5);
            imageViewOne = (ImageView) view.findViewById(R.id.iv_itemone_show);
            mlayoutOne = (LinearLayout) view.findViewById(R.id.ll_itemone_beautybuy);
            buttonOne = (Button) view.findViewById(R.id.bt_item_one);
        }
    }

    public class ViewholderTwo {
        private TextView title1, title2, title3, title4, title5, title6;
        private ImageView imageViewTwo;
        private LinearLayout mlayoutTwo;
        private Button buttonTwo;

        public ViewholderTwo(View view) {
            title1 = (TextView) view.findViewById(R.id.tv_itemtwo_title1);
            title2 = (TextView) view.findViewById(R.id.tv_itemtwo_title2);
            title3 = (TextView) view.findViewById(R.id.tv_itemtwo_title3);
            title4 = (TextView) view.findViewById(R.id.tv_itemtwo_title4);
            title5 = (TextView) view.findViewById(R.id.tv_itemtwo_title5);
            title6 = (TextView) view.findViewById(R.id.tv_itemtwo_title6);
            imageViewTwo = (ImageView) view.findViewById(R.id.iv_itemtwo_show);
            mlayoutTwo = (LinearLayout) view.findViewById(R.id.ll_itemtwo_beautybuy);
            buttonTwo = (Button) view.findViewById(R.id.bt_item_two);
        }
    }
}
